/*提交表单*/
/**
 * 提交表单
 */
function submitFrm(id, url) {
    $(id).attr("action", url);
    $(id).submit();
}

/*前台校验方法*/
/**
 * 按照字符长度校验字符串
 */
function checkStringByLength(o, min, max) {
    if (min == 0 && max == 0) {
        if (o.val().length > 0) {
            return true;
        } else {
            return false;
        }
    } else {
        if (o.val().length > min && o.val().length < max) {
            return true;
        } else {
            return false;
        }
    }
}

/**
 * 按照正则表达式校验字符串
 */
function checkStringByRegex(o, regex) {
    var pattern = new RegExp(regex);
    if (pattern.test(o.val())) {
        return true;
    } else {
        return false;
    }
}

/**
 * 校验数字
 */
function checkNumber(o) {
    var pattern = new RegExp("^[0-9]*$");
    if (pattern.test(o.val())) {
        return true;
    } else {
        return false;
    }
}

/**
 * 校验日期
 */
function checkDate(o) {
    var pattern = new RegExp("^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$");
    if (pattern.test(o.val())) {
        return true;
    } else {
        return false;
    }
}

/**
 * 校验时间
 */
function checkTime(o) {
    var pattern = new RegExp("^([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$");
    if (pattern.test(o.val())) {
        return true;
    } else {
        return false;
    }
}

/**
 * 校验日期和时间
 */
function checkDatetime(o) {
    var pattern = new RegExp("^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)\\s([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$");
    if (pattern.test(o.val())) {
        return true;
    } else {
        return false;
    }
}

/*数据表格插件*/
/**
 * 数据表格插件
 */
(function ($) {
    $.fn.extend({
        initDataTable: function (options) {
            var settings = $.extend({
                rowSelected: false,
                colSelected: false,
                data: null
            }, options);
            return this.each(function () {
                var _this = $(this);
                _this.find("tr:first").addClass("columnhead");
                _this.find("tr:not(:first):even").addClass("roweven");
                _this.find("tr:odd").addClass("rowodd");

                if (settings.rowSelected) {
                    if (settings.colSelected) {
                        _this.find("td").click(function () {
                            id = $(this).parent().index();
                            id2 = $(this).index();
                            _this.trigger("rowselected", [settings.data[id - 1], id, id2]);
                        });
                        _this.find("tr:not(:first)").click(function () {
                            $(this).addClass("rowselected").siblings().removeClass("rowselected");
                        });
                    } else {
                        _this.find("tr:not(:first)").click(function () {
                            $(this).addClass("rowselected").siblings().removeClass("rowselected");
                            id = $(this).index();
                            _this.trigger("rowselected", [settings.data[id - 1], id]);
                        });
                    }
                    _this.find("tr:not(:first)").mouseenter(function () {
                        $(this).addClass("hover");
                    });
                    _this.find("tr:not(:first)").mouseleave(function () {
                        $(this).removeClass("hover");
                    });
                } else {
                    _this.find("tr:not(:first)").hover(function () {
                        $(this).attr("bColor", $(this).css("background-color")).css("background-color", "#c9e5ff").css("cursor", "pointer");
                    }, function () {
                        $(this).css("background-color", $(this).attr("bColor"));
                    });
                }
            });
        },
        setSelectedRow: function (options) {
            var settings = $.extend({
                rowNumber: null
            }, options);
            return this.each(function () {
                var _this = $(this);
                if (settings.rowNumber != null) {
                    _this.find("tr").eq(settings.rowNumber).addClass("rowselected").siblings().removeClass("rowselected");
                } else {
                    _this.find("tr").removeClass("rowselected");
                }
            });
        }
    });
})(jQuery);

/*消息对话框插件*/
/**
 * 表单对象
 */
var msgForm;

/**
 * 消息对话框(0:确定,1:确定/取消)
 */
(function ($) {
    $.fn.extend({
        messagedialog: function (options) {
            var settings = $.extend({
                title: null,
                message: null,
                type: 0,
                width: 0,
                height: 0,
                modal: false
            }, options);
            return this.each(function () {
                $(this).append("<p style='text-align:center;line-height:" + settings.height / 3 + "px;' >" + settings.message + "</p>");
                if (settings.type == 0) {
                    $(this).dialog({
                        title: settings.title,
                        message: settings.message,
                        buttons: [{
                            text: "确定", click: function () {
                                if (msgForm != null) {
                                    msgForm.submit();
                                }
                                $(this).dialog("close");
                            }
                        }],
                        modal: settings.modal,
                        width: settings.width,
                        height: settings.height,
                        autoOpen: false,
                        resizable: false,
                    });
                } else {
                    $(this).dialog({
                        title: settings.title,
                        message: settings.message,
                        width: settings.width,
                        height: settings.height,
                        buttons: [{
                            text: "确定", click: function () {
                                if (msgForm != null) {
                                    msgForm.submit();
                                }
                                $(this).dialog("close")
                                ;
                            }
                        },
                            {
                                text: "取消", click: function () {
                                    $(this).dialog("close");
                                }
                            }],
                        modal: settings.modal,
                        autoOpen: false,
                        resizable: false,
                    });
                }
            });
        },
        openMessageDialog: function (options) {
            var settings = $.extend({
                message: null
            }, options);
            return this.each(function () {
                if (settings.message != null) {
                    $(this).find("p").text(settings.message);
                }
                $(this).dialog("open");
            });
        }
    });
})(jQuery);

/**
 * 打开消息对话框
 */
function openMessageDialog(id) {
    $(id).dialog("open");
}

/**
 * 打开消息对话框
 */
function openMessageDialog(id, frm) {
    msgForm = $(frm);
    $(id).dialog("open");
}

/**
 * 打开消息对话框
 */
function openMessageDialog(id, frm, url) {
    msgForm = $(frm);
    if (url != null && url.length > 0) {
        msgForm.attr("action", url);
    }
    $(id).dialog("open");
}

/*自定义表单对话框插件*/
(function ($) {
    $.fn.extend({
        initCustomFrmDialog: function (options) {
            var settings = $.extend({
                title: null,
                width: 0,
                height: 0,
                type: null,
                modal: false,
                frm: null,
                validation: false
            }, options);
            return this.each(function () {
                if (settings.type != null) {
                    $(this).dialog({
                        title: settings.title,
                        width: settings.width,
                        height: settings.height,
                        buttons: [{
                            text: settings.type, click:
                                function () {
                                    settings.frm.submit();
                                }
                        },
                            {
                                text: "取消", click: function () {
                                    $(this).dialog("close");
                                }
                            }],
                        modal: settings.modal,
                        autoOpen: false,
                        resizable: false,
                    });
                } else {
                    $(this).dialog({
                        title: settings.title,
                        width: settings.width,
                        height: settings.height,
                        buttons: [{
                            text: "取消", click: function () {
                                $(this).dialog("close");
                            }
                        }],
                        modal: settings.modal,
                        autoOpen: false,
                        resizable: false,
                    });
                }
            });
        },
        openCustomFrmDialog: function (options) {
            var settings = $.extend({
                title: null,
                frm: null,
                validation: false
            }, options);
            return this.each(function () {
                if (settings.title != null) {
                    $(this).dialog({
                        title: settings.title
                    });
                }
                if (settings.validation) {
                    settings.frm.validationEngine("hide");
                    settings.frm.validationEngine();
                }
                $(this).dialog("open");
            });
        }
    });
})(jQuery);

(function ($) {
    $.fn.extend({
        initCustomDialog: function (options) {
            var settings = $.extend({
                title: null,
                width: 0,
                height: 0,
                button1: null,
                data1: null,
                disabled1: false,
                button2: null,
                data2: null,
                disabled2: false,
                button3: null,
                data3: null,
                disabled3: false,
                buttonSize: 2,
                auto: true,
                modal: false,
            }, options);
            return this.each(function () {
                if (settings.buttonSize == 1) {
                    $(this).dialog({
                        title: settings.title,
                        width: settings.width,
                        height: settings.height,
                        buttons: [
                            {
                                text: settings.button1,
                                disabled: settings.disabled1,
                                click: function () {
                                    if (settings.auto) {
                                        $(this).dialog("close");
                                    }
                                    $(this).trigger("btnClick", [settings.button1, settings.data1]);
                                },
                            }
                        ],
                        modal: settings.modal,
                        autoOpen: false,
                        resizable: false,
                    });
                } else if (settings.buttonSize == 2) {
                    $(this).dialog({
                        title: settings.title,
                        width: settings.width,
                        height: settings.height,
                        buttons: [
                            {
                                text: settings.button1,
                                disabled: settings.disabled1,
                                click: function () {
                                    if (settings.auto) {
                                        $(this).dialog("close");
                                    }
                                    $(this).trigger("btnClick", [settings.button1, settings.data1]);
                                }
                            },
                            {
                                text: settings.button2,
                                disabled: settings.disabled2,
                                click: function () {
                                    if (settings.auto) {
                                        $(this).dialog("close");
                                    }
                                    $(this).trigger("btnClick", [settings.button2, settings.data2]);
                                }
                            }
                        ],
                        modal: settings.modal,
                        autoOpen: false,
                        resizable: false,
                        data: null
                    });
                } else if (settings.buttonSize == 3) {
                    $(this).dialog({
                        title: settings.title,
                        width: settings.width,
                        height: settings.height,
                        buttons: [
                            {
                                text: settings.button1,
                                disabled: settings.disabled1,
                                click: function () {
                                    if (settings.auto) {
                                        $(this).dialog("close");
                                    }
                                    $(this).trigger("btnClick", [settings.button1, settings.data1]);
                                }
                            },
                            {
                                text: settings.button2,
                                disabled: settings.disabled2,
                                click: function () {
                                    if (settings.auto) {
                                        $(this).dialog("close");
                                    }
                                    $(this).trigger("btnClick", [settings.button2, settings.data2]);
                                }
                            },
                            {
                                text: settings.button3,
                                disabled: settings.disabled3,
                                click: function () {
                                    if (settings.auto) {
                                        $(this).dialog("close");
                                    }
                                    $(this).trigger("btnClick", [settings.button3, settings.data3]);
                                }
                            }
                        ],
                        modal: settings.modal,
                        autoOpen: false,
                        resizable: false,
                    });
                } else {
                    $(this).dialog({
                        title: settings.title,
                        width: settings.width,
                        height: settings.height,
                        modal: settings.modal,
                        autoOpen: false,
                        resizable: false,
                    });
                }
            });
        },
        openCustomDialog: function (options) {
            var settings = $.extend({
                title: null
            }, options);
            return this.each(function () {
                if (settings.title != null) {
                    $(this).dialog({
                        title: settings.title
                    });
                }
                $(this).dialog("open");
            });
        }
    });
})(jQuery);

/*时间日期选择插件*/
var datePicker;

/**
 * 时间日期选择插件
 */
(function ($) {
    $.fn.extend({
        datetimepicker: function (options) {
            var settings = $.extend({
                title: null,
                width: 0,
                height: 0,
                modal: false
            }, options);
            return this.each(function () {
                $(this).append("<table align='center'></table>");
                $(this).find("table").append("<tr><td>起始日期:</td><td><input type='text'  id='datetimeStart'  style='width: 120px;vertical-align:top;'/></td></tr>");
                $(this).find("table").append("<tr><td>结束日期:</td><td><input type='text'  id='datetimeEnd'  style='width: 120px;vertical-align:top;'/></td></tr>");
                $(this).dialog({
                    title: settings.title,
                    width: settings.width,
                    height: settings.height,
                    buttons: [{
                        text: "确定", click: function () {
                            var curDate = $("#datetimeStart").val();
                            var curTime = $("#datetimeEnd").val();
                            var pattern = new RegExp("^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$");
                            if (!pattern.test($("#datetimeStart").val())) {
                                alert("起始日期输入不正确(yyyy-MM-dd)");
                                return;
                            }
                            if (!pattern.test($("#datetimeEnd").val())) {
                                alert("结束日期输入不正确(yyyy-MM-dd)");
                                return;
                            }
                            datePicker.val(curDate + "至" + curTime);
                            $(this).dialog("close");
                        }
                    },
                        {
                            text: "取消", click: function () {
                                $(this).dialog("close");
                            }
                        }],
                    modal: settings.modal,
                    autoOpen: false,
                    resizable: false
                });
                $("#datetimeStart").datepicker({
                    dateFormat: "yy-mm-dd",
                    showOn: "button",
                    buttonImage: "/foc/resources/images/admin/calendar.png",
                    buttonImageOnly: true
                });
                $("#datetimeEnd").datepicker({
                    dateFormat: "yy-mm-dd",
                    showOn: "button",
                    buttonImage: "/foc/resources/images/admin/calendar.png",
                    buttonImageOnly: true
                });
            });
        },
        openDatetimepicker: function (options) {
            var settings = $.extend({
                obj: null,
                text: null
            }, options);
            return this.each(function () {
                $(this).dialog("open");
                if (settings.obj != null) {
                    datePicker = settings.obj;
                }
                if (settings.text != null && settings.text != "") {
                    var values = settings.text.split("至");
                    $(this).find("#datetimeStart").val(values[0]);
                    $(this).find("#datetimeEnd").val(values[1]);
                } else {
                    $(this).find("#datetimeStart").val(getCurDate());
                    $(this).find("#datetimeEnd").val(getCurDate());
                }

                function getCurDate() {
                    var curYear = new Date().getFullYear();
                    var curMonth = (new Date().getMonth() + 1);
                    if (curMonth < 10) {
                        curMonth = "0" + curMonth;
                    }
                    var curDay = new Date().getDate();
                    if (curDay < 10) {
                        curDay = "0" + curDay;
                    }
                    return curYear + "-" + curMonth + "-" + curDay;
                }
            });
        }
    });
})(jQuery);

/**
 * 打开日期时间选择控件
 */
function openDatetimepicker(id, dtp) {
    objDatetimepicker = $(dtp);
}

/**
 * 实时获取员工列表
 */
function getStafferTree(url) {
    try {
        var obj = $.ajax({
            url: url,
            async: false,
            timeout: 60000
        }).responseText;
        return $.parseJSON(obj);
    } catch (e) {
        return null;
    }
}

/**
 * 树形控件
 */
(function ($) {
    $.fn.extend({
        initTree: function (options) {
            var settings = $.extend({
                id: null,
                title: null,
                width: 0,
                height: 0,
                multiSelected: true,
                data: null,
                defaultValue: null,
                modal: false
            }, options);
            return this.each(function () {
                $(this).dialog({
                    title: settings.title,
                    width: settings.width,
                    height: settings.height,
                    buttons: [{
                        text: "确定", click: function () {
                            var name = "";
                            var value = "";
                            $(this).find("input[ name='subNode']").each(function () {
                                if ($(this).prop("checked")) {
                                    var values = $(this).val().split(":");
                                    if (settings.multiSelected) {
                                        name += values[0] + ",";
                                        value += values[1] + ",";
                                    } else {
                                        name += values[0];
                                        value += values[1];
                                    }
                                }
                            });
                            $(this).trigger("selectedTree", [name, value]);
                            $(this).dialog("close");
                        }
                    },
                        {
                            text: "取消", click: function () {
                                $(this).dialog("close");
                            }
                        }],
                    modal: settings.modal,
                    autoOpen: false,
                    resizable: false
                });

                // 初始化父节点
                $(this).find("div").remove();
                for (var i = 0; i < settings.data.length; i++) {
                    $(this).append("<div id='node" + i + "'></div>");
                    $(this).find("#node" + i).append("<a><img src='/foc/resources/images/common/lessen.png' border='0' /></a>" + settings.data[i].name);
                    $(this).find("#node" + i).find("a").click("#node" + i, function (event) {
                        if ($(this).find("img").attr("src") == "/foc/resources/images/common/spread.png") {
                            if (settings.id != null) {
                                id = "#" + settings.id;
                                $(id).find(event.data).find("div").css("display", "");
                            } else {
                                $(event.data).find("div").css("display", "");
                            }
                            $(this).find("img").attr("src", "/foc/resources/images/common/lessen.png");
                        } else {
                            if (settings.id != null) {
                                id = "#" + settings.id;
                                $(id).find(event.data).find("div").css("display", "none");
                            } else {
                                $(event.data).find("div").css("display", "none");
                            }
                            $(this).find("img").attr("src", "/foc/resources/images/common/spread.png");
                        }
                    });

                    // 初始化子节点
                    $(this).find("#node" + i).append("<div></div>");
                    $(this).find("#node" + i).find("div").each(function () {
                        if (settings.data[i].nodes.length > 0) {
                            if (settings.multiSelected) {
                                $(this).append("&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' id='selectedAll' value='' />全选<br>");
                                $(this).find("#selectedAll").click(this, function (event) {
                                    if ($(this).prop("checked")) {
                                        $(event.data).each(function () {
                                            $(this).find("input").prop("checked", true);
                                        });
                                    } else {
                                        $(event.data).each(function () {
                                            $(this).find("input").prop("checked", false);
                                        });
                                    }
                                });
                            }
                            for (var j = 0; j < settings.data[i].nodes.length; j++) {
                                $(this).append("&nbsp;&nbsp;&nbsp;&nbsp;<input name='subNode' type='checkbox' value='" + settings.data[i].nodes[j].name + ":" + settings.data[i].nodes[j].value + "' />" + settings.data[i].nodes[j].name + "<br>");
                                $(this).find("input[name='subNode']").click(i, function (event) {
                                    if (settings.multiSelected) {
                                        if (!$(this).prop("checked")) {
                                            $("#node" + event.data).find("#selectedAll").prop("checked", false);
                                        }
                                    } else {
                                        if ($(this).prop("checked")) {
                                            $(this).siblings().prop("checked", false);
                                            for (var k = 0; k < settings.data.length; k++) {
                                                if (k != event.data) {
                                                    $("#node" + k).find("input[name='subNode']").prop("checked", false);
                                                }
                                            }
                                        }
                                    }
                                });
                            }
                        }
                    });
                }
                if (settings.defaultValue != null) {
                    for (var i = 0; i < settings.data.length; i++) {
                        for (var j = 0; j < settings.data[i].nodes.length; j++) {
                            if (settings.defaultValue.indexOf(",") < 0) {
                                if (settings.data[i].nodes[j].value == settings.defaultValue) {
                                    $(this).find("#node" + i).find("input[name='subNode']").eq(j).prop("checked", true);
                                }
                            } else {
                                values = settings.defaultValue.split(",");
                                for (var k = 0; k < values.length; k++) {
                                    if (settings.data[i].nodes[j].value == values[k]) {
                                        $(this).find("#node" + i).find("input[name='subNode']").eq(j).prop("checked", true);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            });
        },
        openTree: function () {
            $(this).dialog("open");
        },
        closeTree: function () {
            $(this).dialog("close");
        }
    });
})(jQuery);